package com.cust.zy.newtask.dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.cust.zy.newtask.model.Task;
import com.cust.zy.newtask.model.Word;

/**
 * Created by zhuangyuan on 4/15/2017.
 * 邮箱：zhuangyuan.k@qq.com
 */
public class DataOperation {
    public Context context;
    private SqlHelper helper;

    public DataOperation(Context context) {
        this.context = context;
        helper = new SqlHelper(context);
    }

    /*
     * 添加 Task
     *
     * 参数：
     * task：Task
     */
    public void insertIntoTask(Task task) throws Exception {
        ContentValues cv = new ContentValues();
        cv.clear();
        cv.put("ID", task.getID());
        cv.put("NAME", task.getName());
        cv.put("NUM_OF_WORD", task.getNumOfWord());
        cv.put("GENERATE_TIME", task.getGenerateTime());
        cv.put("DESCRIPTION", task.getDescription());
        cv.put("TYPE", task.getType());
        helper.Insert(context, SqlHelper.TASK_TABLE, cv);
        Log.i("insert", task.getID());
    }

    /*
     * 修改 Task
     *
     * 参数：
     * task：Task
     */
    public void AlterTask(Task task) throws Exception {
        ContentValues cv = new ContentValues();
        cv.clear();
        cv.put("NAME", task.getName());
        cv.put("DESCRIPTION", task.getDescription());
        helper.Update(context, SqlHelper.TASK_TABLE, cv,"ID = ?",new String[]{task.getID()});
        Log.i("alter", task.getID());
    }
    /*
    *  delete Task
    *
    * 参数：
    * task：Task
    */
    public void deleteTask(String taskID) throws Exception {
        helper.Delete(context, SqlHelper.WORD_TABLE," TASK_ID = ? ",new String[]{taskID});
        helper.Delete(context, SqlHelper.TASK_TABLE," ID = ? ",new String[]{taskID});
        Log.i("delete task", taskID);
    }

    /*
     *查询Task
     *参数
     *selection：查询条件（where语句）
     *selectionArgs:where中包含的“？”
     */
    public ArrayList<Task> queryTask(String selection, String[] selectionArgs) {

        Cursor cursor = helper.Query(context, SqlHelper.TASK_TABLE, null, selection, selectionArgs, null, null, null);
        ArrayList<Task> list = new ArrayList<Task>();
        if (cursor.moveToFirst()) {
            do {
                Task task = new Task();
                task.setID(cursor.getString(0));
                task.setName(cursor.getString(1));
                task.setNumOfWord(cursor.getInt(2));
                task.setGenerateTime(cursor.getString(3));
                task.setDescription(cursor.getString(4));
                task.setType(cursor.getInt(5));
                list.add(task);
            }
            while (cursor.moveToNext());
        }
        Log.i("query", list.size() + "");
        cursor.close();
        return list;
    }

    /*
     * 添加Word
     *
     * 参数：
     * word：Word
     */
    public void insertIntoWord(Word word) throws Exception {
        ContentValues cv = new ContentValues();
        cv.clear();
        cv.put("ID", word.getID());
        cv.put("NAME", word.getName());
        cv.put("TRANSLATION", word.getTranslation());
        cv.put("PHONETIC_ALPHABET", word.getPhonetic_alphabet());
        cv.put("LIST", word.getList());
        cv.put("TASK_ID", word.getTaskID());
        helper.Insert(context, SqlHelper.WORD_TABLE, cv);
    }

    /*
     *查询Word
     *参数
     *selection：查询条件（where语句）
     *selectionArgs:where中包含的“？”
     */
    public ArrayList<Word> queryWord(String selection, String[] selectionArgs) {

        Cursor cursor = helper.Query(context, SqlHelper.WORD_TABLE, null, selection, selectionArgs, null, null, null);
        ArrayList<Word> list = new ArrayList<Word>();
        if (cursor.moveToFirst()) {
            do {
                Word word = new Word();
                word.setID(cursor.getString(0));
                word.setName(cursor.getString(1));
                word.setTranslation(cursor.getString(2));
                word.setPhonetic_alphabet(cursor.getString(3));
                word.setList(cursor.getString(4));
                word.setTaskID(cursor.getString(5));
                list.add(word);
            }
            while (cursor.moveToNext());
        }
        cursor.close();
        return list;
    }

    /*
     * 在生词本中添加生词
     *
     * 参数：
     * word：Word
     */
    public void insertIntoWordNote(Word word) throws Exception {
        ContentValues cv = new ContentValues();
        cv.clear();
        cv.put("ID", word.getID());
        cv.put("NAME", word.getName());
        cv.put("TRANSLATION", word.getTranslation());
        cv.put("PHONETIC_ALPHABET", word.getPhonetic_alphabet());
        cv.put("LIST", word.getList());
        cv.put("TASK_ID", word.getTaskID());
        helper.Insert(context, SqlHelper.WORD_NOTE_TABLE, cv);
    }

    /*
     *查询生词本
     *参数
     *selection：查询条件（where语句）
     *selectionArgs:where中包含的“？”
     */
    public ArrayList<Word> queryWordNote(String selection, String[] selectionArgs) {

        Cursor cursor = helper.Query(context, SqlHelper.WORD_NOTE_TABLE, null, selection, selectionArgs, null, null, null);
        ArrayList<Word> list = new ArrayList<Word>();
        if (cursor.moveToFirst()) {
            do {
                Word word = new Word();
                word.setID(cursor.getString(0));
                word.setName(cursor.getString(1));
                word.setTranslation(cursor.getString(2));
                word.setPhonetic_alphabet(cursor.getString(3));
                word.setList(cursor.getString(4));
                word.setTaskID(cursor.getString(5));
                list.add(word);
            }
            while (cursor.moveToNext());
        }
        cursor.close();
        return list;
    }
}
